import requests
import json
import time
import configparser
import get_coin_info

config = configparser.ConfigParser()
config.read('config.ini', encoding='GB18030')
acc_token = ''

def get_access_token():
    global acc_token
    url = "https://api.weixin.qq.com/cgi-bin/token?"
    params = {
        'grant_type': 'client_credential',
        'appid' : config.get("wechat_pub", "appID"),
        'secret' : config.get("wechat_pub", "appsecret")
    }

    res = requests.request("GET", url, params=params)
    acc_token = json.loads(res.text).get("access_token")
    expiry = json.loads(res.text).get("expires_in")

def send_message(msg="Empty info!"):
    global acc_token
    url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + acc_token

    params = {
        'touser' : config.get("wechat_pub", "user"),
        'msgtype' : 'text',
        'text' : {
                'content' : msg
        }
    }
    res = requests.request("POST", url, data=json.dumps(params))
    print(res)

cnt = 0
while(1):
    if (cnt % 6 == 0):
        get_access_token()
    send_message(get_coin_info.coin_info())
    time.sleep(3600)
    cnt += 1